package com.starblink.android.basic.network.interceptor

import com.apollographql.apollo3.api.ApolloRequest
import com.apollographql.apollo3.api.ApolloResponse
import com.apollographql.apollo3.api.Error
import com.apollographql.apollo3.api.Operation
import com.apollographql.apollo3.interceptor.ApolloInterceptor
import com.apollographql.apollo3.interceptor.ApolloInterceptorChain
import com.starblink.android.basic.ext.toastUI
import com.starblink.android.basic.network.base.BaseApiCodeAndUrl
import com.starblink.android.basic.network.base.SkGqlClient.uploadErrorMsg
import com.starblink.android.basic.user.UserDataCenter
import com.starblink.basic.util.CommUtils
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.onEach

class ErrorApolloInterceptor() : ApolloInterceptor {

    override fun <D : Operation.Data> intercept(
        request: ApolloRequest<D>,
        chain: ApolloInterceptorChain
    ): Flow<ApolloResponse<D>> {
        return chain.proceed(request).onEach { response ->
            if (response.errors != null) {
                var error: Error = response.errors!!.first()
                var extensions: Map<String, Any?>? = error.extensions
                if (extensions != null) {
                    var code = extensions!!["code"]
                    //code为空 则为网络异常 如参数传错
                    if (code != null) {
                        when (code) {
                            BaseApiCodeAndUrl.ACCOUNT_99999 -> handleCode9999()
                            BaseApiCodeAndUrl.TOKEN_OUT_OF_DATE -> handleTokenOutOfDate()
                            BaseApiCodeAndUrl.ERROR_BUSY -> toastUI(CommUtils.getString(com.starblink.basic.style.R.string.network_busy))
                            BaseApiCodeAndUrl.ACCOUNT_UU000001 -> {
                                toastUI(CommUtils.getString(com.starblink.basic.style.R.string.account_deleted))
                                UserDataCenter.clearToken()
                            }

                            BaseApiCodeAndUrl.ACCOUNT_00009 -> toastUI("${error.message}")
                            BaseApiCodeAndUrl.SYS_BUSY_00001 -> toastUI("${error.message}")
                            BaseApiCodeAndUrl.UW000001 -> toastUI("${error.message}")
                            BaseApiCodeAndUrl.ACCOUNT_00012 -> {} //外面处理
                            BaseApiCodeAndUrl.SEND_EMAIL_TIME_OVER_LIMIT -> {} //外面处理
                            BaseApiCodeAndUrl.CODE_311015004 -> {} //外面处理
                            BaseApiCodeAndUrl.CODE_311014001 -> {
                                //不是异常 不弹出 CommonMutateManage 处理
                            } //外面处理
                            BaseApiCodeAndUrl.CODE_311015001 -> {
                                //不是异常 不弹出 CommonMutateManage 处理
                            } //外面处理
                            "201012001" -> {
                            }

                            else -> {
                                when (error.message) {
                                    "The wishList name already exists" -> {
                                        toastUI("The board name already exists")
                                    }

                                    else -> {
                                        toastUI("${error.message}")
                                    }
                                }
                            }
                        }
                    } else {
                        uploadErrorMsg(tag = "ErrorApolloInterceptor","${error.message}")
                    }
                } else {
                    toastUI(CommUtils.getString(com.starblink.basic.style.R.string.network_error))
                }
            }

        }

    }

    private fun handleCode9999() {
        toastUI("BaseApiCode.ACCOUNT_99999")
    }

    private fun handleTokenOutOfDate() {
        toastUI("BaseApiCode.TOKEN_OUT_OF_DATE")
    }


}